package com.example.john.foodstore;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class AddDishes extends AppCompatActivity {
    public  final int RESULT_LOAD_IMAGE=1;
    public Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_dishes);
    }
    public void Confirm(View v){
        Intent intent=new Intent();
        intent.setClass(AddDishes.this, MyDishes.class);
        EditText t1=(EditText)findViewById(R.id.ed1);
        String txt=String.valueOf(t1.getText());
        // Log.i("Amy", txt);
        intent.putExtra("dishesName", String.valueOf(((EditText) findViewById(R.id.ed1)).getText()));
        intent.putExtra("dishesClass", String.valueOf(((EditText) findViewById(R.id.ed2)).getText()));
        intent.putExtra("dishesPrice", String.valueOf(((EditText) findViewById(R.id.ed3)).getText()));
        intent.putExtra("dishesPicture",bitmap);
        //AddDishes.this.startActivity(intent);
        // startActivityForResult(intent,0);
        setResult(0, intent);
        AddDishes.this.finish();

    }

    public void ConveyImg(View view){
        Intent intent1 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent1, RESULT_LOAD_IMAGE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==RESULT_LOAD_IMAGE&&resultCode==RESULT_OK&&data!=null) {
            Uri uri = data.getData();
            Log.i("hm","2222222222222");
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            Log.i("hm", "2222222222222");
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturesPath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView=(ImageView)findViewById(R.id.img);
            bitmap=BitmapFactory.decodeFile(picturesPath);
            imageView.setImageBitmap(bitmap);



        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_dishes, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
